package com.example.strategyproject.strategy;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author ycd0075
 * @date 2024-01-25 20:49
 */
@Component
public class StrategyManager2 implements ApplicationContextAware {
    private final Map<String, IPromotionStrategy> processorHashMap = new HashMap<>(16);


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        //返回指定类型和子类型的所有bean
        Map<String, IPromotionStrategy> map = BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, IPromotionStrategy.class);
        if (!map.isEmpty()) {
            map.values().stream().forEach(v -> processorHashMap.put(v.getType(), v));
        }
        System.out.println("processorHashMap: " + processorHashMap);
    }
}
